<?php

namespace app\shopapi\model\pay;


use app\admin\model\User;
use app\common\library\Sms as Smslib;
use app\shopapi\model\Shop;
use app\shopapi\model\ShopBills;
use fast\Http;



//商户进件
class ShopInfeed
{

    public $client_id = 'testsit';
    public $client_secret = 'EguwEckByf2I6u6z';
    //---------------------------------附件类型----------------------------------
    //ID_CARD_FRONT 身份证正⾯
    //ID_CARD_BEHIND 身份证反⾯
    //BUSINESS_LICENCE 营业执照照⽚
    //BANK_CARD 银行卡
    //AGREE_MENT 协议
    //OPENING_PERMIT 开户许可证
    //CHECKSTAND_IMG 收银台照片(仅对公进件需传)
    //SHOP_OUTSIDE_IMG 上传门头照片(仅对公进件需传)
    //SHOP_INSIDE_IMG 商铺内部照片(仅对公进件需传)
    //OTHERS 其他无对应类型图片，请传其他类型
    //SETTLE_ID_CARD_FRONT 结算人身份证人像面
    //SETTLE_ID_CARD_BEHIND 结算人身份证国徽面
    //LETTER_OF_AUTHORIZATION 法人授权涵

    //请求获取token
    public function getToken()
    {
        //测试公网地址
        $api = 'https://test.wsmsd.cn/sit/htkauth/oauth/token';
        //测试地址 http://htkauth-sit.lakala.sh.in/oauth/token
        //⽣产地址 https://tkapi.lakala.com/auth/oauth/token

        $data = [
            'grant_type'=>'client_credentials',
            'client_id'=>$this->client_id,
            'client_secret'=>$this->client_secret,
        ];

        $res = Http::sendRequest($api,$data);

        $result = json_decode($res['msg'],true);

        return $result;
    }

    //地区信息
    public function getArea()
    {
        //测试环境内网：
        //$api = 'http://htkactvi-sit.lakala.sh.in/registration/api/registration/organization';
        //测试环境外网：
        $api = 'https://test.wsmsd.cn/sit/htkregistration/organization';

        //生产环境：
        //$api = 'https://htkactvi.lakala.com/registration/organization';

        $data = [
            'code'=>'',
            'name'=>'',
            'parentCode'=>1
        ];

        $token = $this->getToken();

        $header = [
            'Authorization: bearer'.$token['access_token']
        ];

        $res = $this->curl($api,$data,'get',$header);
        dump($res);die;
        $result = json_decode($res,true);

        return $result;
    }

    //银行信息
    public function getBank()
    {
        //测试环境内网：
        //$api = 'http://htkactvi-sit.lakala.sh.in/registration/api/registration/organization/bank';
        //测试环境外网：
        $api = 'https://test.wsmsd.cn/sit/htkregistration/organization/bank';
        //生产环境：
        //$api = 'https://htkactvi.lakala.com/registration/organization/bank';

        $data = [
            'code'=>'',
            'name'=>'',
            'parentCode'=>1
        ];

        $token = $this->getToken();

        $header = [
            'Authorization: bearer'.$token['access_token']
        ];

        $res = $this->curl($api,$data,'get',$header);
        dump($res);die;
        $result = json_decode($res,true);

        return $result;
    }

    //银行列表
    public function getBankList()
    {
        //测试环境内网：
        //$api = 'http://htkactvi-sit.lakala.sh.in/registration/api/registration/bank';
        //测试环境外网：
        $api = 'https://test.wsmsd.cn/sit/htkregistration/bank';
        //生产环境：
        //$api = 'https://htkactvi.lakala.com/registration/bank';

        $data = [
            'areaCode'=>'',//地区编码
            'bankName'=>'',//银行名称
        ];

        $token = $this->getToken();

        $header = [
            'Authorization: bearer'.$token['access_token']
        ];

        $res = $this->curl($api,$data,'get',$header);
        dump($res);die;
        $result = json_decode($res,true);

        return $result;
    }

    //文件上传
    public function upload($file,$imgType)
    {
        //测试环境内网：
        //$api = 'http://htkactvi-sit.lakala.sh.in/registration/api/registration/file/upload';
        //测试环境外网：
        $api = 'https://test.wsmsd.cn/sit//htkregistration/file/upload';
        //生产环境：
        //$api = 'https://htkactvi.lakala.com/registration/file/upload';

        $token = $this->getToken();

        $header = [
            'Authorization: bearer'.$token['access_token']
        ];
        $data = [
            'file'=>$file,
            'imgType'=>$imgType,
            'sourcechnl'=>'0',//0:PC,1:安卓,2:IOS
        ];

        $res = $this->curl($api,$data,'post',$header);
        dump($res);die;
        $result = json_decode($res,true);

        return $result;
    }

    //商户进件
    public function apply($shop_id)
    {
        $shop_info = Shop::get($shop_id);

        //测试环境:
        $api =  'https://test.wsmsd.cn/sit/htkregistration/merchant';
        //⽣产环境: https://htkactvi.lakala.com/registration/merchant (POST)
        //账户类型,1对公账户,0对私账户
        if ($shop_info['bank_type']==1){
            $accountType = 57;
        } else {
            $accountType = 58;
        }

        if ($shop_info['subject_type']==1){
            //个体工商户
            $merType = 'TP_MERCHANT';
        } elseif($shop_info['subject_type']==2){
            //个人 小微
            $merType = 'TP_MERCHANT';
        } else {
            //企业
            $merType = 'TP_MERCHANT';
        }

        $data = [
            'userNo'=>'',//商户归属用户信息-必传
            'email'=>$shop_info['email'],//商户邮箱-必传
            'busiCode'=>'PAPER_CODE',//业务类型-必传
            'merRegName'=>$shop_info['shop_name'],//商户注册名称-必传
            'merType'=>$merType,//商户注册类型-必传TP_MERCHANT:企业TP_PERSONAL:⼩微个⼈
            'merName'=>$shop_info['shop_name'],//商户名称(经营名称-必传
            'merAddr'=>$shop_info['address'],//商户详细地址-必传
            'provinceCode'=>$shop_info['province_code'],//省代码-必传
            'cityCode'=>$shop_info['city_code'],//市代码-必传
            'countyCode'=>$shop_info['area_code'],//区县代码-必传
            'licenseName'=>$shop_info['business_license_name'],//营业执照名称
            'licenseNo'=>$shop_info['bus_lic_no'],//营业执照号码
            'licenseDtStart'=>$shop_info['business_license_start_dt'],//营业执照开始时间
            'licenseDtEnd'=>$shop_info['business_license_end_dt'],//营业执照过期时间
            'latitude'=>$shop_info['lat'],//经度 进件所在地址经度-必传
            'longtude'=>$shop_info['lng'],//纬度 进件所在地址纬度-必传
            'source'=>'H5',//进件来源APP, H5 （2选1）-必传
            'businessContent'=>'',//商户经营内容-必传
            //'isLegalPerson'=>'true',//是否法⼈进件
            'larName'=>$shop_info['id_card_name'],//法⼈姓名-必传
            'larIdType'=>'01',//法⼈证件类型 01 身份证 暂时只支持身份证-必传
            'larIdCard'=>$shop_info['id_card_num'],//法⼈证件号码-必传
            'larIdCardStart'=>$shop_info['id_card_start_time'],//法⼈证件开始⽇期 格式yyyy-MM-dd-必传
            'larIdCardEnd'=>$shop_info['id_card_end_time'],//法⼈证件过期时间 格式yyyy-MM-dd-必传
            'contactMobile'=>$shop_info['mobile'],//商户联系⼈⼿机号码-必传
            'contactName'=>$shop_info['realname'],//商户联系⼈姓名-必传
            'openningBankCode'=>'',//结算账户开户⾏号-必传
            'openningBankName'=>$shop_info['bank_name'],//结算账户开户⾏名称-必传
            'clearingBankCode'=>'',//结算账户清算⾏号-必传
            'settleProvinceCode'=>'',//结算信息省份代码-必传
            'settleProvinceName'=>'',//结算信息省份名称-必传
            'settleCityCode'=>'',//结算信息城市代码-必传
            'settleCityName'=>'',//结算信息城市名称-必传
            'accountNo'=>$shop_info['bank_num'],//结算人银行卡号-必传
            'accountName'=>$shop_info['id_card_name'],//结算人账户名称-必传
            'accountType'=>$accountType,//结算账户类型 57 对公 58 对私-必传
            //'accountIdType'=>'',//结算⼈证件类型
            'accountIdCard'=>$shop_info['id_card_num'],//结算⼈证件号码-必传
            //'accountIdDtStart'=>$shop_info['id_card_start_time'],//结算⼈证件开始时间
            //'accountIdDtEnd'=>$shop_info['id_card_end_time'],//结算⼈证件过期时间
            //'externalNo'=>'',//外部编号
            //'bizContent'=>'',//业务扩展信息-必传
            //'attchments'=>'',//附件信息集合-必传
            'settleType'=>'D0',//结算类型-必传
            //'shopId'=>'',//⽹点代码
            'settlementType'=>'AUTOMATIC',//结算方式 MANUAL:手动结算(结算至拉卡拉APP钱包),AUTOMATIC:自动结算,REGULAR:定时结算（仅企业商户支持）-必传
            //'regularSettlementTime'=>'',//⽹定时结算时间定时结算必传 格式为 HHmm 例如结算时间为下午3点 传入值为 1500
        ];

        $data['bizContent'] = [
            'termNum'=>'100000',//终端数量
            'fees'=>[
                [
                    'feeCode'=>'CREDIT_CARD',//贷记卡费率
                    'feeValue'=>'0.38'
                ],
                [
                    'feeCode'=>'DEBIT_CARD',//借记卡费率（此费率类型必须上送封顶值 topFee）
                    'feeValue'=>'0.38',
                    'topFee'=>'20'
                ],
                [
                    'feeCode'=>'SCAN_PAY',//扫码费率
                    'feeValue'=>'0.38'
                ],
                [
                    'feeCode'=>'WECHAT',//微信
                    'feeValue'=>'0.38'
                ],
                [
                    'feeCode'=>'ALIPAY',//支付宝
                    'feeValue'=>'0.38'
                ]
            ],//费率集合
            'mcc'=>'',//商户mcc编号
            'activityId'=>'',//归属活动信息
        ];


        $data['attchments'] = [
            [
                'id'=>'',
                'type'=>'ID_CARD_FRONT'
            ],
            [
                'id'=>'',
                'type'=>'ID_CARD_BEHIND'
            ],
            [
                'id'=>'',
                'type'=>'BUSINESS_LICENCE'
            ],
            [
                'id'=>'',
                'type'=>'BANK_CARD'
            ],
            [
                'id'=>'',
                'type'=>'SETTLE_ID_CARD_FRONT'
            ],
            [
                'id'=>'',
                'type'=>'SETTLE_ID_CARD_BEHIND'
            ]
        ];
        if ($shop_info['bank_type']==1){
            array_push($data['attchments'],['id'=>'','type'=>'CHECKSTAND_IMG']);
            array_push($data['attchments'],['id'=>'','type'=>'SHOP_OUTSIDE_IMG']);
            array_push($data['attchments'],['id'=>'','type'=>'SHOP_INSIDE_IMG']);
        }

        $token = $this->getToken();

        $header = [
            'Authorization: bearer'.$token['access_token']
        ];

        $res = $this->curl($api,$data,'post',$header);

        $result = json_decode($res,true);

        return $result;
    }


    private function curl($url, $data = [], $method = 'POST',$header, $time_out = 3)
    {
        $curl = curl_init();
        // 设置curl允许执行的最长秒数

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, $time_out);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }

        // 执行操作
        $result = curl_exec($curl);

        curl_close($curl);
        return $result;
    }
}